﻿using B.S.DataBase.SuYuan.ErrorCode;
using B.S.DataBase.SuYuan.Write.API.Applications.Commands.ShopInfo;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.DataBase.SuYuan.Write.API.Controllers
{
    /// <summary>
    /// api-写
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ManageController : ControllerBase
    {
        private readonly IMediator mediator;

        public ManageController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 上传图片（图片信息表）
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UploadPic(IFormFile file)
        {
            try
            {
                var res = "wwwroot/img/" + file.FileName;
                using (FileStream fs = new FileStream(res, FileMode.OpenOrCreate))
                {
                    file.CopyTo(fs);
                }
                return Ok("https://localhost:7052/img/" + file.FileName);
            }
            catch (Exception)
            {

                throw;
            }
        }


        //商品基本信息模块
        #region
        /// <summary>
        /// 添加商品信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> AddShop(ShopCreateCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 上传图片（商品信息）
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UploadImages(IFormFile file)
        {
            try
            {
                var res = "wwwroot/pic/" + file.FileName;
                using(FileStream fs = new FileStream(res,FileMode.OpenOrCreate))
                {
                    file.CopyTo(fs);
                }
                return Ok("https://localhost:7052/pic/" + file.FileName);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 删除商品信息(批量)
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<APIResult<object>> DelShop(ShopDelCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 删除商品信息(单删)
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIResult<object>> DelDanShop(ShopDanDelCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 修改商品信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIResult<object>> UpdateShop(ShopUpdateCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        ///产品检测报告图片添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> TuPicAdd(TuPicCreateCommand command)
        {
            return await mediator.Send(command);
        }
        #endregion
        //养殖信息
        #region





        #endregion


    }
}
